﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

namespace WebChatServer.Models
{
    public class FriendRepository : IFriendRepository
    {
        AccountEntities _entities = new AccountEntities();
        GroupRepository _groupRepository = new GroupRepository();
        MemberRepository _memberRepository = new MemberRepository();

        public Friend Get(string friendId)
        {
            return null;
        }

        public FriendStatus[] GetFriend(string memberId)
        {

            var lstMem = from mem in _entities.FriendShips
                         where mem.IDMember == memberId
                         select mem;

            if (lstMem != null && lstMem.Count() > 0)
            {
                FriendShip[] friendShip = lstMem.ToArray();
                Member[] friends = new Member[friendShip.Count()];
                FriendStatus[] friendOnls = new FriendStatus[friendShip.Count()];

                for (int i = 0; i < friendShip.Count(); i++)
                {
                    string idfriend = friendShip[i].IDFriend;
                    var lstMem1 = from mem in _entities.Members
                                  where mem.Id == idfriend
                                  select mem;
                    if (lstMem1 != null && lstMem1.Count() > 0)
                    {
                        friends[i] = lstMem1.ToArray()[0];
                        string groupName = _groupRepository.GetGroup(friendShip[i].IDGroup).Name;
                        friendOnls[i] = new FriendStatus() { GroupName = groupName, Id = friendShip[i].IDFriend, Status = 0 };
                    }
                }

                return friendOnls;
            }

            return null;
        }

        public Friend IsExist(Friend friend)
        {
            var lstMem = from mem in _entities.Members
                         where mem.Id == friend.Id
                         select mem;

            if (lstMem != null && lstMem.Count() > 0)
            {
                Member mem = lstMem.ToArray()[0];
                friend.Image.Name = mem.Image;
                friend.Image.Array = GetImage(mem.Image);
                friend.Name = mem.Name;
                return friend;
            }

            return null;
        }

        public Byte[] GetImage(string file)
        {
            if (file != "null")
            {
                string imageName = HttpContext.Current.Server.MapPath("~/" + file);
                if (imageName != null)
                {
                    byte[] total = new byte[0];
                    Stream fileStream = File.Open(imageName, FileMode.Open, FileAccess.Read);
                    if (fileStream != null)
                    {
                        byte[] array = new byte[0];
                        byte[] buffer = new byte[32];
                        int read = 0;
                        while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            array = new byte[total.Length + read];
                            total.CopyTo(array, 0);
                            Array.Copy(buffer, 0, array, total.Length, read);
                            total = array;
                        }
                        fileStream.Close();
                        return total;
                    }
                }
            }
            return new byte[0];
        }

        public Friend AddFriend(Friend friend)
        {
            if ((friend = IsExist(friend)) == null)
                return null;

            if (_groupRepository.isExist(friend.MemberId + "_" + friend.GroupName) == false)
            {
                //add group roi moi add friend
                _groupRepository.AddGroup(new FriendGroup()
                {
                    GroupId = friend.MemberId + "_" + friend.GroupName,
                    Name = friend.GroupName,
                    UserName = friend.MemberId,
                    Friends = null
                });

                FriendShip friendShip = new FriendShip();
                friendShip.IDMember = friend.MemberId;
                friendShip.IDGroup = friend.MemberId + "_" + friend.GroupName;
                friendShip.IDFriend = friend.Id;
                _entities.FriendShips.AddObject(friendShip);
                _entities.SaveChanges();

            }
            else
            {
                FriendShip friendShip = new FriendShip();
                friendShip.IDMember = friend.MemberId;
                friendShip.IDGroup = friend.MemberId + "_" + friend.GroupName;
                friendShip.IDFriend = friend.Id;
                _entities.FriendShips.AddObject(friendShip);
                _entities.SaveChanges();
            }

            return friend;
        }



        public string DeleteFriend(string memberId, string friendId)
        {

            var lstFriend = from frd in _entities.FriendShips
                            where frd.IDMember == memberId && frd.IDFriend == friendId
                            select frd;

            if (lstFriend != null && lstFriend.Count() > 0)
            {
                FriendShip friend = lstFriend.ToArray()[0];
                string groupname = friend.IDGroup;
                _entities.FriendShips.DeleteObject(friend);
                _entities.SaveChanges();

                return groupname;
            }

            return null;
        }

    }
}